﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uComponents.Core.DataTypes.MultiUrlPicker.Dto;
using System.Web;
using uComponents.RazorModels.UrlPicker;
using System.IO;
using System.Web.UI;
using System.Collections;

namespace uComponents.RazorModels.MultiUrlPicker
{
    public class RazorMultiUrlPickerState : MultiUrlPickerState, IEnumerable
    {
        public HtmlString ToHtml()
        {
            return ToHtml(null);
        }
        public HtmlString ToHtml(string cssClassName)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (TextWriter tw = new StreamWriter(ms))
                {
                    using (HtmlTextWriter writer = new HtmlTextWriter(tw))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, cssClassName);
                        writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                        foreach (var innerState in Items)
                        {
                            writer.RenderBeginTag(HtmlTextWriterTag.Li);
                            var state = new RazorUrlPickerState()
                            {
                                Mode = innerState.Mode,
                                NodeId = innerState.NodeId,
                                NewWindow = innerState.NewWindow,
                                Title = innerState.Title,
                                Url = innerState.Url
                            };
                            state.Render(writer, null);
                            writer.RenderEndTag();
                        }
                        writer.RenderEndTag();
                    }
                    tw.Flush();
                    ms.Position = 0;
                    using (TextReader reader = new StreamReader(ms))
                    {
                        string html = reader.ReadToEnd();
                        return new HtmlString(html);
                    }
                }
            }

        }

        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }

        #endregion
    }
}
